package com.mustafaiev.tair.cts.tapestry.pages.baskets;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import com.mustafaiev.tair.cts.exeptions.DataNotRemovedException;
import com.mustafaiev.tair.cts.exeptions.DataNotStoredException;
import com.mustafaiev.tair.cts.facade.IBasketFacade;
import com.mustafaiev.tair.cts.facade.IBasketsBatchFacade;
import com.mustafaiev.tair.cts.factory.ICostTrackingSystemFacadeFactory;
import com.mustafaiev.tair.cts.tapestry.dto.BasketDTO;
import com.mustafaiev.tair.cts.tapestry.dto.BasketsBatchDTO;

public class Index {

	@Inject
	private ICostTrackingSystemFacadeFactory facadeFactory;

	@InjectPage
	private Create createBasket;

	@Property
	private BeanModel<BasketDTO> basketGridModel;

	@Inject
	private BeanModelSource beanModelSource;

	@Inject
	private Messages messages;

	@Component(id = "basketsForm")
	private Form form;

	@Property
	@Persist
	private List<BasketDTO> baskets;

	@Property
	@Persist
	private List<BasketDTO> basketsToChange;

	@Property
	@Persist
	private BasketDTO basket;

	public boolean isCheck() {
		return false;
	}

	public void setCheck(final boolean check) {
		if (check) {
			this.basketsToChange.add(this.basket);
		}
	}

	@SetupRender
	void setupRender() {

		this.basketsToChange = new ArrayList<BasketDTO>();

		this.basketGridModel = this.beanModelSource.createDisplayModel(
				BasketDTO.class, this.messages);

		this.basketGridModel.add("Payer").sortable(true);
		this.basketGridModel.include("id", "date", "type", "marketName",
				"comments", "payer");
		this.baskets = this.facadeFactory.getFacade(IBasketFacade.class)
				.retrieveBaskets();
	}

	@OnEvent(value = "create")
	public Object onCreate() {
		return this.createBasket;
	}

	public Object onSelectedFromTopDelete() {
		return deleteBaskets();
	}

	public Object onSelectedFromBottomDelete() {
		return deleteBaskets();
	}

	public Object onSelectedFromTopAddToBatch() {
		return addToBatch();
	}

	public Object onSelectedFromBottomAddToBatch() {
		return addToBatch();
	}

	private Object addToBatch() {
		final BasketsBatchDTO batchDTO = new BasketsBatchDTO();
		batchDTO.setBaskets(this.basketsToChange);
		try {
			getBatchFacade().save(batchDTO);
		} catch (final DataNotStoredException e) {
			this.form.recordError(e.getLocalizedMessage());
		}
		return null;
	}

	private Object deleteBaskets() {
		try {
			getFacade().deleteBatch(this.basketsToChange);
		} catch (final DataNotRemovedException e) {
			this.form.recordError(e.getLocalizedMessage());
		}
		return null;
	}

	private IBasketFacade getFacade() {
		final IBasketFacade facade = this.facadeFactory
				.getFacade(IBasketFacade.class);
		return facade;
	}

	private IBasketsBatchFacade getBatchFacade() {
		final IBasketsBatchFacade facade = this.facadeFactory
				.getFacade(IBasketsBatchFacade.class);
		return facade;
	}

}
